import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { SystemPostStateType } from './typings'
import {
  getSystemPostList,
  getTenantSelect,
  postSystemPostAdd,
  postSystemPostUpdate,
  postSystemPostRemove,
  getPostSelectListByDeptId
} from '~/api/system/systemPost'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

const systemPost: Module<SystemPostStateType, SystemPostStateType> = {
  namespaced: true,
  state: {
    categorySelectList: [], // 岗位类型 下拉选项
    categoryEnum: {}, // 岗位类型 枚举
    tenantSelectList: [], // 所属租户 下拉选项
    tenantSelectEnum: {}, // 所属租户 枚举
    postSelectList: [], // 所属岗位 下拉列表
    postEnum: {} // 所属岗位 枚举
  },
  getters: {
    categorySelectList: (state) => state.categorySelectList,
    categoryEnum: (state) => state.categoryEnum,
    tenantSelectList: (state) => state.tenantSelectList,
    tenantSelectEnum: (state) => state.tenantSelectEnum,
    postSelectList: (state) => state.postSelectList,
    postEnum: (state) => state.postEnum
  },
  mutations: {
    SET_TENANTSELECTLIST: (state, payload) => {
      state.tenantSelectList = payload
      state.tenantSelectEnum = filterValueEnum(
        payload,
        'tenantId',
        'tenantName'
      ) as SystemPostStateType['tenantSelectEnum']
    },
    SET_CATEGORYSELECTLIST: (state, payload) => {
      state.categorySelectList = payload
      state.categoryEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    },
    SET_POST_SELECT_LIST: (state, payload) => {
      state.postSelectList = payload
      state.postEnum = filterValueEnum(payload, 'id', 'postName')
    }
  },
  actions: {
    // 获取 岗位管理 列表
    GetSystemPostList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getSystemPostList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 所属租户
    GetTenantSelect<S, T>({ commit }: ActionContext<S, T>) {
      return new Promise((resolve, reject) => {
        getTenantSelect()
          .then((res: AxiosResponse) => {
            commit('SET_TENANTSELECTLIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 岗位类型
    GetDataDictionaryByCode<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDataDictionaryByCode<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_CATEGORYSELECTLIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 新增岗位
    PostSystemPostAdd<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postSystemPostAdd<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 编辑岗位
    PostSystemPostUpdate<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postSystemPostUpdate<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 删除岗位
    PostSystemPostRemove<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        postSystemPostRemove<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 根据部门id获取岗位下拉列表
    GetPostSelectListByDeptId<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getPostSelectListByDeptId<T>(params)
          .then((res: AxiosResponse) => {
            commit('SET_POST_SELECT_LIST', res?.data?.data || [])
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    }
  }
}

export default systemPost
